import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
	
public class ShortRangeChart extends JPanel{

    CircleList cList;  //added this
    SpaceTraderGui gui;
    Player player;
    SolarSystem currSystem;
    SolarSystem targSystem;
    private double fuelEfficiency;
    private double maxCurrDistance;

    /**
     * A getter for the user's current system.
     * @return The current system.
     */
    public SolarSystem getCurrSystem() {
		return currSystem;
	}

    /**
     * A setter for the current system (used after a player warps)
     * @param currSystem The updated current system.
     */
	public void setCurrSystem(SolarSystem currSystem) {
		this.currSystem = currSystem;
	}
	public SolarSystem getTargSystem() {
		return targSystem;
	}
	public void setTargSystem(SolarSystem system) {
		targSystem = system;
	}

	/**
	 * Constructor for the Short Range Chart.
	 * @param system The array of systems.
	 * @param gui The GUI
	 * @param player The player object which holds his/her data
	 */
	public ShortRangeChart(ArrayList<SolarSystem> system, SpaceTraderGui gui, Player player) {
    	setBackground(Color.black);
        setPreferredSize(new Dimension(300,300));
        setLayout(null);
        repaint();
        this.player = player;
        this.cList = new CircleList();
        this.gui = gui;
        for (SolarSystem planet:system){
        	cList.add( new Circle(planet));
        }
        addMouseListener(new MouseClick());
    }

	/**
	 * Draws the chart.
	 */
    public void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	for (Circle c : cList.getCircle()){
    		g.setColor(c.color);
    		if(currSystem != null) {
    			if(c.system.equals(currSystem)) {
    				g.setColor(Color.GREEN);
    				g.drawOval(currSystem.getX()-(int)maxCurrDistance, currSystem.getY()-
    				(int)maxCurrDistance, (int)maxCurrDistance*2,(int)maxCurrDistance*2);
    			}
    			if(c.system.equals(targSystem)) {
    				g.setColor(Color.RED);
    			}
    		}
            g.fillOval(c.x, c.y, 7, 7);
            g.drawString(c.name, c.x, c.y);
        }
	}
        
    /**
     * Returns whether or not the target solar system is in range of the ship
     * @param system
     * @return
     */
    public boolean inRange(SolarSystem current, SolarSystem target) {
    	fuelEfficiency = (player.getShip().getMaxDistance() + (player.getPilotPoints()/1.5)) / player.getShip().getMaxFuel();
    	maxCurrDistance = (fuelEfficiency * (player.getShip().getFuel()));
    	if (target != null) {
    		double distance = target.getDistance(current);
    		if(distance < maxCurrDistance) {
        		return true;
        	}
    	}
    	return false;
    }
    
    
    public void updateColor(SolarSystem currSystem) {
    	Graphics g = getGraphics();
    	super.paintComponent(g);
    	repaint();
    	for(Circle c : cList.getCircle()) {
    		if(c.system.equals(currSystem)) {
    			g.setColor(Color.GREEN);
                g.fillOval(c.x, c.y, 7, 7);
                g.drawString(c.name, c.x, c.y);
    		}
    	}
    }
    
    private class MouseClick implements MouseListener { //the interface needs all of this
		public void mousePressed (MouseEvent event) {}
		public void mouseReleased (MouseEvent event) {}
		public void mouseEntered (MouseEvent event) {}
		public void mouseExited (MouseEvent event) {}
		public void mouseClicked(MouseEvent event) {
			Point coord = event.getPoint();
			SolarSystem system = cList.click((int)coord.getX(), (int)coord.getY());
			if (system!=null){
				gui.updateTargetSystem(system);
				gui.getChartPanel().setTargSystem(system);
				//gui.updateMarketPlace(player);
				//call function in GUI that'll update target universe info
			}
		}//end mouseClicked
	}//end MouseClick
    
    class Circle{
    	public SolarSystem system;
        public int x, y, rad;
        public String name;
        public Color color;
        public Circle(SolarSystem system){ //int x, int y, String name){
        	this.system =system;
        	this.color = Color.white;
        	this.x=system.getX();
        	this.y=system.getY();
        	this.name = system.getName();
        	this.rad = 7;//player.getPilotPoints()+player.getShip().getFuel();
        }
    }
    
     class CircleList{
      ArrayList<Circle> circles;  
      	public CircleList(){
	        circles = new ArrayList<Circle>();
	      }
	      
	      public void add(Circle circ){
	    	  circles.add(circ);
	      }
	
	      public ArrayList<Circle> getCircle(){
	    	  return circles;
	     }
	      public SolarSystem click(int x, int y){
	    	  SolarSystem system = null;
	    	  for (Circle circ:circles){
	    		  if ( ((x > circ.x) &&  (x < circ.x+circ.rad)) && ((y > circ.y) && (y < circ.y+circ.rad))){
	    			  system = circ.system;
	    		  }
	    		  
	    	  }
	    	 return system;
	      }
     }
}
